package com.ljxt.dt.util.thl;


import com.ljxt.dt.dao.basicinformationdao.RowMapper;
import com.ljxt.dt.util.DaoUtil;

import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 糖糖
 * @version 1.0.0
 * @date 2020/5/12 17:40
 * @copyright 老九学堂
 */
public class ThlUtils {


    /**
     * 获得连接
     *
     * @return 返回一个连接
     */
    public static Connection getConnection() {
        try {
            return DaoUtil.getConnection();
        } catch (Exception e) {
            System.out.println("获得connection失败！");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获得执行sql的PreparedStatement对象
     *
     * @param connection
     * @param sql
     * @param params
     * @return
     */
    public static PreparedStatement getPreparedStatement(Connection connection, String sql, Object... params) throws Exception {

        //获取执行sql的对象
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //设置参数
        if (params != null && params.length > 0) {
            //当参数不为空且参数的长度大于0时才可以设置参数
            for (int i = 0; i < params.length; i++) {
                //给sql语句的？位置赋值
                preparedStatement.setObject(i + 1, params[i]);
            }
        }

        return preparedStatement;
    }

    /**
     * 查询出来的对象保存为一个集合
     * @param sql
     * @param rowMapper
     * @param params
     * @param <T>
     * @return
     */
    public static <T> List<T> executeQuery(String sql, RowMapper<T> rowMapper, Object... params) {
        //声明一个泛型集合，用来保存查询到的对象
        List<T> list = new ArrayList<>();

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            //获得连接
            connection = getConnection();
           // connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/dt", "root", "123456");

            //调用方式执行sql
            preparedStatement = getPreparedStatement(connection, sql, params);
            //执行查询方法获得结果集
            resultSet = preparedStatement.executeQuery();

            //遍历结果集
            while (resultSet.next()) {
                //把结果集里面的每一个对象赋完值后添加到集合中
                list.add(rowMapper.mapRow(resultSet));
            }
            System.out.println("ThlUtil查询打印的sql为：" + sql);
            System.out.println(params);
        } catch (Exception e) {

            System.out.println("executeQuery查询出错");
            e.printStackTrace();
        } finally {
            try {
                //归还连接到数据池
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException("归还连接到数据池失败！" + e);
            }
        }
        return list;
    }

    /**
     * 只查询一个对象
     * @param <T>
     * @param sql
     * @param rowMapper
     * @param params
     * @return
     */
    public static <T> T executeQueryOne(String sql, RowMapper<T> rowMapper, Object... params) {

        T rowData = null;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = getConnection();
           // connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/dt", "root", "123456");

            preparedStatement = getPreparedStatement(connection, sql, params);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                rowData = rowMapper.mapRow(resultSet);
            }
        } catch (Exception e) {
            System.out.println("executeQueryOne查询出错");
            e.printStackTrace();
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return rowData;
    }

    /**
     * 查询一个字段出来
     * @param sql
     * @param params
     * @return
     */
    public static Object executeQueryUnique(String sql, Object... params) {
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            conn = getConnection();

           // conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/dt", "root", "123456");
            preparedStatement = getPreparedStatement(conn, sql, params);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                return resultSet.getObject(1);
            }
        } catch (Exception e) {
            System.out.println("executeQueryUnique查询出错");
            e.printStackTrace();

        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 删除方法
     * @param sql
     * @param params
     * @return
     */
    public static int executeUpdate(String sql, Object...params){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = getConnection();
            preparedStatement = getPreparedStatement(connection, sql, params);
            int rows = preparedStatement.executeUpdate();
            return rows;
        } catch (Exception e) {
            System.out.println("executeUpdate查询出错");
            e.printStackTrace();
        } finally {
            try {
                //归还连接到连接池
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }




}
